package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.KfnProjectUserVo;
import com.ruoyi.system.domain.bo.KfnProjectUserBo;
import com.ruoyi.system.service.IKfnProjectUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目下的用户
 *
 * @author Panda
 * @date 2023-12-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/projectUser")
public class KfnProjectUserController extends BaseController {

    private final IKfnProjectUserService iKfnProjectUserService;

    /**
     * 查询项目下的用户列表
     */
    @SaCheckPermission("system:projectUser:list")
    @GetMapping("/list")
    public TableDataInfo<KfnProjectUserVo> list(KfnProjectUserBo bo, PageQuery pageQuery) {
        return iKfnProjectUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目下的用户列表
     */
    @SaCheckPermission("system:projectUser:export")
    @Log(title = "项目下的用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KfnProjectUserBo bo, HttpServletResponse response) {
        List<KfnProjectUserVo> list = iKfnProjectUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目下的用户", KfnProjectUserVo.class, response);
    }

    /**
     * 获取项目下的用户详细信息
     *
     * @param projectUserId 主键
     */
    @SaCheckPermission("system:projectUser:query")
    @GetMapping("/{projectUserId}")
    public R<KfnProjectUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long projectUserId) {
        return R.ok(iKfnProjectUserService.queryById(projectUserId));
    }

    /**
     * 新增项目下的用户
     */
    @SaCheckPermission("system:projectUser:add")
    @Log(title = "项目下的用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KfnProjectUserBo bo) {
        return toAjax(iKfnProjectUserService.insertByBo(bo));
    }

    /**
     * 修改项目下的用户
     */
    @SaCheckPermission("system:projectUser:edit")
    @Log(title = "项目下的用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KfnProjectUserBo bo) {
        return toAjax(iKfnProjectUserService.updateByBo(bo));
    }

    /**
     * 删除项目下的用户
     *
     * @param projectUserIds 主键串
     */
    @SaCheckPermission("system:projectUser:remove")
    @Log(title = "项目下的用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectUserIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] projectUserIds) {
        return toAjax(iKfnProjectUserService.deleteWithValidByIds(Arrays.asList(projectUserIds), true));
    }
}
